<?php

namespace app\index\model;

use think\Model;

/**
 * Created by PhpStorm.
 * User: su
 * Date: 2017/12/28
 * Time: 16:07
 */
class Logis extends Model
{
    /**
     * $onClick 为点击次数
     * $ifs 为用户端的类型 0 为安卓1 为ios
     */
    public function addLogs($ifs = 0, $id)
    {

        $time = date('Ymd', time());
        $log_id = $this->getLogsId('log_id',$time, $id);
        $data = array();
        //当ID 为空的时候
        if (empty($log_id)) {

            if ($ifs == 1) {
                $android = 1;
                $ios = 0;
            } else {
                $android = 0;
                $ios = 1;
            }
            $data['link_id'] = $id;
            $data['addtime'] = $time;
            $data['ios_download'] = $ios;
            $data['android_download'] = $android;
            return $this->insert($data);
        }

        //ID 不为空时
        $log_id = $log_id->toArray()['log_id'];
        $log_info = $this->getLogsId('',$time, $id)->toArray();

        if ($ifs == 1) {
            $android = 1 + intval($log_info['android_download']);
            $ios = intval($log_info['ios_download']);
        } else {
            $android = intval($log_info['android_download']);
            $ios = 1 + intval($log_info['ios_download']);
        }
        $data['ios_download'] = $ios;
        $data['android_download'] = $android;
        return $this->where('log_id', $log_id)->update($data);

    }

    public function getLogsId($field = null, $time, $id)
    {
        $data['addtime'] = $time;
        $data['link_id'] = $id;
        return $this->field($field)->where($data)->find();

    }

    public function getDataList()
    {


        return $this->alias('l')
            ->join('tp_link k', 'l.link_id = k.link_id')
            ->order('l.addtime desc')->select();

    }
}
